package scantask.containers;

import gov.nasa.worldwind.layers.AirspaceLayer;

import java.util.Map;

import scantask.client.threeDObjects.PolygonePool;
import scantask.client.threeDObjects.exceptions.PolygoneCreationException;
import scantask.containers.exceptions.IllegaleRadiusException;
import scantask.containers.exceptions.InvalidSyntheticObjectTypeException;
import scantask.containers.exceptions.MissingLayerException;
import scantask.containers.exceptions.UnInitializeObjectException;

public class SyntheticObject extends InformationObject{
	
	private String _type;
	private int _radius;

	public SyntheticObject(Coordinate coordinate, ZoomLevel viewInterval, String type, int radius) throws UnInitializeObjectException, IllegaleRadiusException, InvalidSyntheticObjectTypeException {
		super(coordinate, viewInterval);
		
		if (type.compareTo("") == 0) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new SyntheticObject illegal type");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new SyntheticObject illegal type");
			throw new InvalidSyntheticObjectTypeException();
		}
		if (radius <= 0) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new SyntheticObject illegal radius");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new SyntheticObject illegal radius");
			throw new IllegaleRadiusException();
		}
		
		this._type = type;
		this._radius = radius;
	}
	
	public SyntheticObject(Coordinate coordinate, ZoomLevel viewInterval, PolygonePool pool, String type, int radius) throws UnInitializeObjectException, IllegaleRadiusException, InvalidSyntheticObjectTypeException {
		super(coordinate, viewInterval, pool);
		
		if (type.compareTo("") == 0) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new SyntheticObject illegal type");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new SyntheticObject illegal type");
			throw new InvalidSyntheticObjectTypeException();
		}
		if (radius <= 0) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new SyntheticObject illegal radius");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new SyntheticObject illegal radius");
			throw new IllegaleRadiusException();
		}
		
		this._type = type;
		this._radius = radius;
	}
	
	@Override
	public String getType() {
		return "SyntheticObject" + "\t" + this._type;
	}
	
	@Override
	public String getCleanType() {
		return this._type;
	}
	
	public int getRadius() {
		return this._radius;
	}
	
	public void setType(String type) throws InvalidSyntheticObjectTypeException {
		if (type.compareTo("") == 0) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed set SyntheticObject illegal type");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed set SyntheticObject illegal type");
			throw new InvalidSyntheticObjectTypeException();
		}
		
		this._type = type;
	}
	
	public void setRadius(int radius) throws IllegaleRadiusException {
		if (radius <= 0) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed set SyntheticObject illegal radius");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed set SyntheticObject illegal radius");
			throw new IllegaleRadiusException();
		}
		
		this._radius = radius;
	}

	@Override
	public void buildDisplayedObject(PolygonePool polygonePool) throws PolygoneCreationException {
		this._polygone = polygonePool.getPolygone(this.getType(), this.getRadius());
		this.setDefaultPolygoneAccordingly();
	}
	
	private void setDefaultPolygoneAccordingly() {
		this._polygone.setLocation(this.getCoordinate());
	}

	@Override
	public void init3DObject(Map<String, AirspaceLayer> layers) throws MissingLayerException {
		String type = "SyntheticObject";
		AirspaceLayer layer = layers.get(type);
		if (layer == null) {
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Missing layer SyntheticObject exception");
			throw new MissingLayerException();
		}
		this._polygone.init3DObject(layer);
		this._polygone.setDescription(this.getToolTipDescription());
	}

	@Override
	public String getToolTipDescription() {
		return this.getCleanType();
	}

}
